package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.*;
import com.qf.service.DtsBrandService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/22 15:44
 * description:
 */
@Service
public class DtsBrandServiceImpl implements DtsBrandService {

    @Autowired
    DtsBrandMapper brandMapper;

    @Autowired
    DtsGoodsMapper goodsMapper;
    @Override
    public List<Integer> queryByAdminId(Integer id) {
        LambdaQueryWrapper<DtsBrand> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getAdminId,id);
        qw.select(DtsBrand::getId);
        List<DtsBrand> dtsBrands = brandMapper.selectList(qw);
        List<Integer> ids = dtsBrands.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());
        return ids;
    }

    @Override
    public List<Map<String, Object>> findBrand() {

        LambdaQueryWrapper<DtsBrand> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getDeleted,false);

        List<DtsBrand> brands = brandMapper.selectList(qw);
        List<Map<String, Object>> maps = brands.stream().map(brand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", brand.getId());
            map.put("label", brand.getName());
            return map;
        }).collect(Collectors.toList());
        return maps;
    }

    @Override
    public IPage<DtsBrand> queryPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsBrand> ipage=new Page<>(page,limit);
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("deleted",false);
        if(id!=null){
            qw.like("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equals("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        brandMapper.selectPage(ipage,qw);
        return ipage;

    }

    @Override
    public Object insert(DtsBrandVo brand) {
        //数据校验
        if (brand==null){
            return ResponseUtil.fail(-1,"参数不正确");
        }
        if(StringUtils.isEmpty(brand.getName())){
            return ResponseUtil.fail(-1,"品牌商名称不能为空");
        }

        Integer[] categoryIds = brand.getCategoryIds();

        if(categoryIds==null||categoryIds.length==0){
            return ResponseUtil.fail(-1,"请选择主营类目");
        }
        //验证品牌商是否存在
        QueryWrapper<DtsBrand> qw=new QueryWrapper<>();
        qw.eq("name",brand.getName());
        DtsBrand dtsBrand = brandMapper.selectOne(qw);
        if(dtsBrand!=null){
            if(!dtsBrand.getDeleted()){
                return ResponseUtil.fail(-1,"该厂商已存在");
            }else {
                //若已存在删除原有的再新增
                brandMapper.deleteById(dtsBrand);
            }
        }
        //添加品牌商
        DtsBrand brand1=new DtsBrand();
        brand1.setAddTime(new Date());
        brand1.setUpdateTime(new Date());
        brand1.setDeleted(false);
        brand1.setName(brand.getName());
        brand1.setDesc(brand.getDesc());
        brand1.setPicUrl(brand.getPicUrl());
        brand1.setSortOrder(1);
        brand1.setFloorPrice(brand.getFloorPrice());
        brand1.setShareUrl(brand.getShareUrl());
        brand1.setAdminId(brand.getAdminId());
        brand1.setDefaultCategoryId(categoryIds[1]);
        brandMapper.insert(brand1);

        return ResponseUtil.ok(brand1);

    }

    @Override
    public Object update(DtsBrandVo brand) {
        //数据校验
        if (brand==null){
            return ResponseUtil.fail(-1,"参数不正确");
        }
        if(StringUtils.isEmpty(brand.getName())){
            return ResponseUtil.fail(-1,"品牌商名称不能为空");
        }

        Integer[] categoryIds = brand.getCategoryIds();

        if(categoryIds==null||categoryIds.length==0){
            return ResponseUtil.fail(-1,"请选择主营类目");
        }
        //添加商品
        DtsBrand brand1=new DtsBrand();
        brand1.setId(brand.getId());
        brand1.setUpdateTime(new Date());
        brand1.setName(brand.getName());
        brand1.setDesc(brand.getDesc());
        brand1.setPicUrl(brand.getPicUrl());
        brand1.setFloorPrice(brand.getFloorPrice());
        brand1.setShareUrl(brand.getShareUrl());
        brand1.setAdminId(brand.getAdminId());
        brand1.setDefaultCategoryId(categoryIds[1]);
        brandMapper.updateById(brand1);
        return ResponseUtil.ok(brand1);

    }

    @Override
    public Object delete(DtsBrand brand) {
        QueryWrapper<DtsGoods> qw1=new QueryWrapper<>();
        qw1.eq("brand_id",brand.getId());
        //判断品牌商是否被商品关联
        long count=goodsMapper.selectCount(qw1);
        if(count>0){
            return ResponseUtil.fail(-1,"该品牌商有商品在售，暂无法删除");
        }
        brand.setDeleted(true);
        brandMapper.updateById(brand);
        return ResponseUtil.ok();
    }
}
